<?php

if (!defined('MOODLE_INTERNAL')) {
    die('Direct access to this script is forbidden.');
}

global $CFG;
require_once($CFG->dirroot . '/blocks/formal_langs/block_formal_langs.php');
require_once($CFG->dirroot . '/blocks/formal_langs/simpletest/common.php');
require_once($CFG->dirroot . '/blocks/formal_langs/langs_code/predefined/block_formal_langs_json_language.php');

class block_formal_langs_json_test extends UnitTestCase {

    private $langdbrecord;
    private $lang = null;
    private $strings = array();

    public function setUp() {
        $this->langdbrecord = new Object();
        $this->langdbrecord->id = 1;
        $this->langdbrecord->version = 1;
        $this->langdbrecord->parser_enabled = true;
        $this->lang = new block_formal_langs_json_language($this->langdbrecord);
    }

    public function test_lang_constructor() {
        $this->assertNotNull($this->lang);
        $this->assertTrue($this->lang->could_parse());
        $this->assertTrue($this->lang->is_predefined());
        $this->assertEqual($this->lang->name(), 'json');
    }

    public function test_tokenize_1() {
        $processedstring = new block_formal_langs_processed_string();
        $processedstring->string = '{"a" : 1}';
        $stream = $this->lang->tokenize($processedstring);
        $result = $processedstring->tokenstream->tokens;
        
        $this->assertEqual(count($result), 5, 'There must be 5 lexemes in  \'the dog bites fly.\'');
        
        // save processed string
        $this->strings['test_tokenize_1'] = $processedstring;
    }

    public function test_parse() {
        $this->assertTrue(array_key_exists('test_tokenize_1', $this->strings), 'test_tokenize was successefull, we have correct tokenstream here');
        $processedstring = $this->strings['test_tokenize_1'];

        $result = $this->lang->parse($processedstring, false);

        $this->assertNotNull($result, 'There must be a AST, not null.');
    }
}
 ?>